package com.nand.blockdemo.adapter;

import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseItemDraggableAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.lxj.xpopup.XPopup;
import com.nand.blockdemo.R;

import java.util.List;

public class FeekEditAdapter extends BaseItemDraggableAdapter<String, BaseViewHolder> {


    public FeekEditAdapter(@Nullable List<String> data) {
        super(R.layout.item_feedback_edit_layout, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, String item) {
        if (!item.equals("add")) {
            ImageView imageView = helper.getView(R.id.imageView);
            Glide.with(mContext).load(item).into(imageView);
            // 必须在事件发生前，调用这个方法来监视View的触摸
            XPopup.Builder builder = new XPopup.Builder(mContext)
                    .watchView(imageView);

            imageView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {
                    return setOnItemLongClick(builder, view, helper.getLayoutPosition());
                }
            });

            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    setOnItemClick(view, helper.getLayoutPosition());
                }
            });
        }
    }

    private OnItemLongClickListener mOnItemLongClickListener;

    @NonNull
    @Override
    public List<String> getData() {
        return super.getData();
    }

    public boolean setOnItemLongClick(XPopup.Builder builder, View v, int position) {
        return mOnItemLongClickListener.onItemLongClick(FeekEditAdapter.this, builder, v, position);
    }

    public interface OnItemLongClickListener {
        boolean onItemLongClick(BaseQuickAdapter adapter, XPopup.Builder builder, View view, int position);
    }

    public void setOnItemLongClickListener(OnItemLongClickListener listener) {
        mOnItemLongClickListener = listener;
    }
}
